/**
 * Project Name:alqsoft-ms
 * File Name:BaseBeanUtils.java
 * Package Name:com.alqsoft.util
 * Date:2016年10月18日下午5:08:07
 * Copyright (c) 2016, chenzhou1025@126.com All Rights Reserved.
 *
*/

package com.alqsoft.util;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import com.alibaba.fastjson.JSON;
import com.alqsoft.bean.BaseHeader;
import com.alqsoft.bean.BaseInfo;

/**
 * ClassName:BaseBeanUtils <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2016年10月18日 下午5:08:07 <br/>
 * @author   张灿
 * @version  处理信息模板工具类
 * @since    JDK 1.8
 * @see 	 
 */
public class BaseBeanUtils {
	
	private static Charset charset = Charset.forName("UTF-8");
	
	private static BaseInfo baseInfo;
	
	public static void loadBaseInfo(BaseInfo baseInfos){
		 baseInfo=baseInfos;
	}
	/**
	 * 包装发送的内容
	 * @param content 内容
	 * @param accepterId 接受者标识
	 * @param type 发送的文件类型 
	 */
	public static BaseInfo integrated(String content,Long accepteId,Long senderId){
		baseInfo.setContent(content);
		baseInfo.setAccepteId(accepteId);
		baseInfo.setEvent(1);
		baseInfo.setSenderId(senderId);
		return baseInfo;
	}
	/**
	 * 
	 */
	public static BaseInfo LoginBaseInfo(Long senderId,String password){
		baseInfo.setAccepteId(-1l);
		baseInfo.setEvent(0);
		baseInfo.setSenderId(senderId);
		baseInfo.setPassword(password);
		return baseInfo;
	}
	/**
	 * 计算baseinfo的长度
	 * @throws UnsupportedEncodingException 
	 */
	public static int lengSizeInit(BaseInfo baseInfo){
		String s = JSON.toJSONString(baseInfo);
		byte[] bys = new byte[0];
		try {
			bys = s.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return bys.length;
	}
	/**
	 * 计算报文大小
	 * @param baseInfo
	 * @return
	 */
    public static ByteBuffer lengByteBuffer(BaseInfo baseInfo){
    	ByteBuffer bb = ByteBuffer.allocate(4+BaseBeanUtils.lengSizeInit(baseInfo));
    	bb.putInt(BaseBeanUtils.lengSizeInit(baseInfo));
    	bb.put(charset.encode(JSON.toJSONString(baseInfo)));
    	bb.position(0);
    	return bb;
    }
	/**
	 * 对传送的实体进行加密
	 * @param baseHeader
	 * @return
	 */
	public static boolean encrySign(BaseHeader baseHeader){
		return true;
	}
	/**
	 * 对传送的实体进行解密
	 * @return
	 */
	public static boolean decrySign(BaseHeader baseHeader){
		return true;
	}
}
